//
//  HYDateHandle.m
//  Agency
//
//  Created by Jack on 2017/11/21.
//  Copyright © 2017年 胡勇. All rights reserved.
//

#import "HYDateHandle.h"

@implementation HYDateHandle

+(NSDate *)dateFromString:(NSString *)dateStr format:(NSString *)format{
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:format];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    return date;
}

+ (NSString *)distanceTimeWithBeforeTime:(long)beforeTimestamp{
    
    NSString *suffix = @"";
    NSString *result = @"";
    NSDate *referenceDate = [NSDate dateWithTimeIntervalSince1970:beforeTimestamp];
    NSInteger differTime = [[NSDate date] timeIntervalSinceDate:referenceDate];
    if (differTime < 0) {
        
        suffix = @"当前";
    }
    
    float dayDifferent = floor(differTime / (60 * 60 * 24));
    
    int days = (int)dayDifferent;
    int weeks = ceil(dayDifferent / 7);
    int months = ceil(dayDifferent / 30);
    int years = ceil(dayDifferent / 365);
    
    //今天内
    if (dayDifferent <= 0) {
        
        if (differTime < 30) {
            
            result = @"刚刚";
        }
        else if (differTime < 90){
            
            result = @"一分钟前";
        }
        else if (differTime < 30 * 60){
            
            result = @"半个小时前";
        }
        else if (differTime < 60 * 60){
            
            result = @"1个小时前";
        }
        else {
            
            result = [NSString stringWithFormat:@"%d个小时前",(int)floor(differTime / 3600)];
        }
        
    }
    else if (days <= 1){
        
        result = @"昨天";
    }
    else if (days <= 2){
        
        result = @"前天";
    }
    else if (days < 7){
        
        result = [NSString stringWithFormat:@"%d天前",days];
    }
    else if (weeks < 4){
        
        result = [NSString stringWithFormat:@"%d周前",weeks];
    }
    else if (months < 12){
        
        result = [NSString stringWithFormat:@"%d月前",months];
    }
    else{
        
        result = [NSString stringWithFormat:@"%d前",years];
    }
    
    
    return result;
}

+ (NSString *)weekStrFromDate:(NSDate *)currentDate{
    
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijin"];
    [calendar setTimeZone:timeZone];
    NSCalendarUnit unit = NSCalendarUnitWeekday;
    NSDateComponents *components = [calendar components:unit fromDate:currentDate];
    NSArray *weekArr = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
//    NSArray *weekArr = @[@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday"];
    return [weekArr objectAtIndex:components.weekday - 1];
    
}

+ (NSString *)weekStrFromDateComponents:(NSDateComponents *)dateComponents{
    
    //    NSArray *weekArr = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
    NSArray *weekArr = @[@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday"];
    return [weekArr objectAtIndex:dateComponents.weekday - 1];
}

+ (NSString *)getCurrentDateStrYYYYMMDDWithTimeInterval:(NSInteger)timeInterval{
    
    NSDate *date = [NSDate date]; // 获得时间对象
//    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
//    NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
    NSDate *currentDate = [date dateByAddingTimeInterval:timeInterval];// 然后把差的时间加上,就是当前系统准确的时间
    
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [dateFormatter stringFromDate:currentDate];
    return dateStr;
}

+ (NSString *)getCurrentDateStrYYYYMMddHHmmWithTimeInterval:(NSInteger)timeInterval{
    
    NSDate *date = [NSDate date]; // 获得时间对象
//    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
//    NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
    NSDate *currentDate = [date dateByAddingTimeInterval: timeInterval];// 然后把差的时间加上,就是当前系统准确的时间
    
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateStr = [dateFormatter stringFromDate:currentDate];
    return dateStr;
}

+ (NSString *)getCurrentDateStrYYYYMMddHHmmssWithTimeInterval:(NSInteger)timeInterval{
    
    NSDate *date = [NSDate date]; // 获得时间对象
    //    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
    //    NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
    NSDate *currentDate = [date dateByAddingTimeInterval: timeInterval];// 然后把差的时间加上,就是当前系统准确的时间
    
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr = [dateFormatter stringFromDate:currentDate];
    return dateStr;
}

+ (NSString *)getCurrentDateStrHHmmWithTimeInterval:(NSInteger)timeInterval{
    
    NSDate *date = [NSDate date]; // 获得时间对象
//    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
//    NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
    NSDate *currentDate = [date dateByAddingTimeInterval:timeInterval];// 然后把差的时间加上,就是当前系统准确的时间
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"HH"];
    NSString *hourStr = [dateFormatter stringFromDate:currentDate];
    [dateFormatter setDateFormat:@"mm"];
    NSString *minuteStr = [dateFormatter stringFromDate:currentDate];
    if ([minuteStr intValue] <= 30) {
        minuteStr = @"30";
    }
    else{
        minuteStr = @"00";
        hourStr = [NSString stringWithFormat:@"%.2d",[hourStr intValue]+1];
    }
    NSString *dateStr = [NSString stringWithFormat:@"%@:%@",hourStr,minuteStr];
    return dateStr;
}

+ (NSString *)getCurrentDateStrDDMMYYYYWithTimeInterval:(NSInteger)timeInterval{
    
    NSDate *date = [NSDate date]; // 获得时间对象
//    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
//    NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
    NSDate *currentDate = [date dateByAddingTimeInterval:timeInterval];// 然后把差的时间加上,就是当前系统准确的时间
    
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [dateFormatter stringFromDate:currentDate];
    return dateStr;
}

+ (NSString *)getDateStringWithDate:(NSDate *)date formater:(NSString *)formatterStr{
    
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:formatterStr];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    return dateStr;
}

+(BOOL)isSameWithDateA:(NSDate *)dataA dataB:(NSDate *)dataB formater:(NSString *)formatterStr{
    NSString *dateAStr = [HYDateHandle getDateStringWithDate:dataA formater:formatterStr];
    NSString *dateBStr = [HYDateHandle getDateStringWithDate:dataB formater:formatterStr];
    return [dateAStr isEqualToString:dateBStr];
}

@end
